package com.outstanding.his.pretreat.facade;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;
import com.outstanding.his.pretreat.domain.Predrug;
import com.outstanding.his.pretreat.domain.Pretreat;
import com.outstanding.his.pretreat.service.IPretreatService;

@Controller
@SessionAttributes("patient")
public class PretreatController {

	private IPretreatService pretreatService;
	private IPatientService patientService;

	public void setPretreatService(IPretreatService pretreatService) {
		this.pretreatService = pretreatService;
	}

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping(value = "/pretreat/new.do", method = RequestMethod.GET)
	public String pretreatNewForm(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/pretreat/new";
	}

	@RequestMapping("/pretreat/submit.do")
	public String pretreatSubmit(@ModelAttribute("pretreat") Pretreat pretreat,
			@ModelAttribute("patient") Patient patient,
			HttpServletRequest request){
		if (patient.getPretreat() != null) {
			pretreat.setPretreatId(patient.getPretreat().getPretreatId());
		}
		patient.setPretreat(pretreat);
		pretreat = pretreatService.submitPretreat(pretreat);
		// patientService.updatePatient(patient);
		// System.out.println("here");
		String[] drugNames = request.getParameterValues("drugName");
		String[] drugDoses = request.getParameterValues("drugDose");
		String[] morningDoses = request.getParameterValues("morningDose");
		String[] noonDoses = request.getParameterValues("noonDose");
		String[] nightDoses = request.getParameterValues("nightDose");
		List<Predrug> predrugs = patient.getPredrugs();
		int predrugSize = predrugs.size();
		for (int i = 0; i < predrugSize; i++) {
			this.pretreatService.deletePredrug(predrugs.get(i));
		}
		patient.getPredrugs().clear();
		// List<Predrug> predrugs = patient.getPredrugs();
		// int predrugSize = predrugs.size();
		for (int i = 0; i < drugNames.length; i++) {
			Predrug predrug = new Predrug();
			predrug.setDrugName(drugNames[i]);
			predrug.setDrugDose(drugDoses[i]);
			predrug.setMorningDose(morningDoses[i]);
			predrug.setNoonDose(noonDoses[i]);
			predrug.setNightDose(nightDoses[i]);
			/*
			 * if (i < predrugSize) {
			 * System.out.println(patient.getPredrugs().size()+":size");
			 * 
			 * PropertyUtils.copyProperties(patient.getPredrugs().get(i),
			 * predrug);
			 * System.out.println("here: "+patient.getPredrugs().get(i)
			 * .getPredrugId()); }
			 */
			// predrug.setPatient(patient);
			patient.getPredrugs().add(predrug);
			// predrug = pretreatService.createPredrug(predrug);
		}
		
		patientService.updatePatient(patient);
		return "redirect:/pretreat/view.do?patientId=" + patient.getPatientId();
	}

	@RequestMapping("/pretreat/view.do")
	public String pretreatView(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/pretreat/view";
	}

	@RequestMapping(value = "/pretreat/edit.do", method = RequestMethod.GET)
	public String pretreatEdit(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/pretreat/edit";
	}

}
